package com.qzdsoft.erpcloud.service.com.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.basic.BasicUser;
import com.qzdsoft.erpcloud.domain.com.ComStaff;
import com.qzdsoft.erpcloud.mapper.basic.BasicUserMapper;
import com.qzdsoft.erpcloud.mapper.com.ComStaffMapper;
import com.qzdsoft.erpcloud.service.com.StaffService;
import com.qzdsoft.erpcloud.vo.com.StaffInfo;
import com.qzdsoft.erpcloud.vo.com.StaffQueryParam;
import com.qzdsoft.utils.StringUtil;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;

@Service
public class StaffServiceImpl implements StaffService {
    private final static Logger logger = LoggerFactory.getLogger(StaffServiceImpl.class);
    @Autowired
    private ComStaffMapper comStaffMapper;
    @Autowired
    private BasicUserMapper basicUserMapper;



    @Override
    public StaffInfo getById(Integer staffId) {
        return comStaffMapper.selectStaffInfoById(staffId);
    }

 

 
    @Override
    @Transactional
    public Response deleteStaff(Integer staffId) {
//        return Response.getSuccess(comStaffMapper.deleteByPrimaryKey(staffId));
    		int staffcount = comStaffMapper.deleteByPrimaryKey(staffId);
    		if(staffcount!=1) {
    			logger.error("根据主键id删除员工信息com_staff表失败,id={}",staffId);
    			throw new RuntimeException("删除员工信息失败");
    		}
    		int deluser =  basicUserMapper.deleteByPrimaryKey(staffId);
    		if(deluser!=1) {
    			logger.error("根据主键id删除员工信息basic_user表失败,id={}",staffId);
    			throw new RuntimeException("删除员工信息失败");
    		}
    		return Response.success("删除成功!");
    }

	
    @Override
    public List<StaffInfo> getAllStaff(Integer companyId){
        List<StaffInfo> returnList = this.comStaffMapper.getStaffDataByComId(companyId);
        return returnList;
    }



	@Override
	public LayTableResponse selectData(StaffQueryParam param) {
		PageHelper.startPage(param.getPage(), param.getLimit());
		List lists = comStaffMapper.selectData(param.getName(),param.getCompanyId());
		LayTableResponse result = new LayTableResponse(lists);
		return result;
	}

	@Override
	@Transactional
	public Response edit(StaffInfo staff) {
		if(!StringUtil.isEmpty(staff.getId())) {//编辑保存
			ComStaff cs = comStaffMapper.selectByPrimaryKey(staff.getId());
			if(cs == null) {
				logger.error("员工信息修改保存错误，根据id没有找到相应的员工信息,id=",staff.getId());
				throw new RuntimeException("修改员工信息有误，可能员工信息不存在！");
			}
			cs.setDepartmentId(staff.getDepartmentId());
			if(!StringUtil.isEmpty(staff.getNo())) {
				cs.setNo(staff.getNo());
			}
			if(!StringUtil.isEmpty(staff.getPost())) {
				cs.setPost(staff.getPost());
			}
			int count = comStaffMapper.updateByPrimaryKey(cs);
			if(count == 1) {
				logger.debug("员工信息表ComStaff更新成功:id={}",cs.getId());
				BasicUser user = basicUserMapper.selectByPrimaryKey(cs.getId());
				if(user == null) {
					logger.error("员工信息修改保存错误，根据id没有找到相应的BaiscUser表信息,id=",cs.getId());
					throw new RuntimeException("修改员工信息有误，可能员工信息不存在!");
				}
				if(!StringUtil.isEmpty(staff.getName())) {
					user.setName(staff.getName());
				}
				if(!StringUtil.isEmpty(staff.getPhone())) {
					user.setPhone(staff.getPhone());
				}
				if(!StringUtil.isEmpty(staff.getEmail())) {
					user.setEmail(staff.getEmail());
				}
				if(!StringUtil.isEmpty(staff.getPassword())) {
					user.setPassword(SysUtils.string2MD5(staff.getPassword()));
				}
				if(!StringUtil.isEmpty(staff.getRoleId())) {
					user.setRoleId(staff.getRoleId());
				}
				if(!StringUtil.isEmpty(staff.getCompanyId())) {
					user.setCompanyId(staff.getCompanyId());
				}
				if(!StringUtil.isEmpty(staff.getRemark())) {
					user.setRemark(staff.getRemark());
				}
				int userCount = basicUserMapper.updateByPrimaryKey(user);
				if(userCount!=1) {
					logger.error("员工信息根据主键id更新BasicUser表的条目不对，id=",cs.getId());
					throw new RuntimeException("修改员工信息有误，员工信息可能存在重复数据!");
				}
				return Response.success("修改成功");	
			}else{
				logger.error("员工信息根据主键id更新ComStaff表的条目不对，id=",cs.getId());
				throw new RuntimeException("修改员工信息有误，员工信息可能存在重复数据!");
			}
		}else{//新增
			
		
			//basic_user
			BasicUser user = new BasicUser();
			user.setCtime(new Date());
			user.setName(staff.getName());
			user.setPhone(staff.getPhone());
			user.setEmail(staff.getEmail());
			if(!StringUtil.isEmpty(staff.getPassword())) {
				user.setPassword(SysUtils.string2MD5(staff.getPassword()));
			}else {
				user.setPassword(SysUtils.string2MD5("123456"));
			}
			user.setRoleId(staff.getRoleId());
			user.setCompanyId(staff.getCompanyId());
			user.setStatus("1");
			user.setType("1");
			user.setRemark(staff.getRemark());
			int ucount = basicUserMapper.insert(user);
			if(ucount!=1) {
				logger.error("员工信息新增basic_user表错误,user=",user);
				throw new RuntimeException("员工信息新增失败!");
			}
			logger.debug("员工信息新增name={}",staff.getName());
			//com_staff
			ComStaff comStaff = new ComStaff();
			comStaff.setId(user.getId());
			comStaff.setDepartmentId(staff.getDepartmentId());
			comStaff.setNo(staff.getNo());
			comStaff.setPost(staff.getPost());
			int count = comStaffMapper.insert(comStaff);
			if(count!=1) {
				logger.error("员工信息新增失败comStaff=",comStaff);
				throw new RuntimeException("员工信息新增失败,userId="+user.getId());
			}
			return Response.success("新增成功");
		}
	}




	@Override
	public List<StaffInfo> getStaffList(Integer deptId, Integer companyId) {
		
		return comStaffMapper.selectStaffListByDeptId(deptId, companyId);
	}
    
    
    
    
}
